package com.cxl.demo.controller;

import com.cxl.demo.service.BuyHistoryService;
import com.cxl.demo.util.TimeUpd;
import com.cxl.demo.vo.BuyHistory;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/buyHistory")
@CrossOrigin
public class BuyHistoryController {
    @Autowired
    BuyHistoryService buyHistoryService;
    @Autowired
    User user;
    @Autowired
    ReturnData returnData;


    @PostMapping("GoodsGetBuyHistoryList")
    public ReturnData GoodsGetBuyHistoryList(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        Integer goodsId = (Integer) map.get("goodsId");
        map.put("page", ((Integer) map.get("currentPage") - 1) * (Integer) map.get("pageSize"));
        map.put("size", (Integer) map.get("pageSize"));
        returnData.setData(buyHistoryService.findGoodsBuyHistory(map));
        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;
    }


    @PostMapping("UserGetBuyHistoryList")
    public ReturnData UserGetBuyHistoryList(@RequestBody Map<String, Object> map) {
        returnData.setData(null);
        map.put("page", ((Integer) map.get("currentPage") - 1) * (Integer) map.get("pageSize"));
        map.put("size", (Integer) map.get("pageSize"));
        map.put("buyGoodsUserId", user.getUserId());
        returnData.setData(buyHistoryService.findUserBuyHistory(map));

        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;
    }


    @PostMapping("BuyGoods")
    public ReturnData BuyGoods(@RequestBody Map<String, Object> map) {
        returnData.setData(null);
//        Date date= new Date();
        String timestamp = new TimeUpd().getDate();
        Integer userId = user.getUserId();
        Integer goodsId = (Integer) map.get("GoodsBuyId");
        Integer buyGoodsNumber = Integer.parseInt((String) map.get("buyGoodsNumber"));
        BuyHistory buyHistory = new BuyHistory();
        buyHistory.setBuyGoodsNumber(buyGoodsNumber);
        buyHistory.setBuyTime(timestamp);
        buyHistory.setGoodsId(goodsId);
        buyHistory.setBuyGoodsUserId(userId);
        Integer ok = buyHistoryService.addBuyHistory(buyHistory);
        if (ok == 1) {
            returnData.setStatus(true);
            returnData.setMsg("兑换商品成功");
        } else {
            returnData.setStatus(false);
            returnData.setMsg("兑换商品失败");
        }
        return returnData;
    }


    @PostMapping("DelHistory")
    public ReturnData DelHistory(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        Integer ok = buyHistoryService.delBuyHistory((Integer) map.get("buyHistoryId"));
        if (ok == 1) {
            returnData.setStatus(true);
            returnData.setMsg("取消兑换商品成功");
        } else {
            returnData.setStatus(false);
            returnData.setMsg("取消兑换商品失败,已发货");
        }

        return returnData;
    }
}
